/**
 * 房间游戏
 */
var RoomPlayLayer = cc.Layer.extend({
    //realy_bg
    realy_bg:null,
    realy_size:null,
    realy_user_face_1:null,//1,2,3,4 分别为自己，右侧，对面，上侧
    ws_user_1:null,ws_user_2:null,ws_user_3:null,ws_user_4:null,//1,2,3,4分别为自己，下家，对面，上家
    sprite:null,
    PPP_pai:new Array(),
    fapaitu:new Array(),
    CHK_SXJ:null,
    MP_P:null,
    MP_K:null,
    DUN_H:null,
    BTN_PENG:null,
    BTN_CHI:null,
    BTN_CLOSE:null,
    BTN_HU:null,
    DIPAI_NUM:null,
    MOPAI_BG:null,
    MOPAI_BG2:null,
    MOPAI_YANGAI:null,
    temp_trip:new Array(),
    //初始化结构函数
    ctor:function () {
        this._super();
        this.realy_size = cc.director.getWinSize();
        //重设左侧中心点( 取当前屏幕中心点 - 3墩牌的宽度 + 牌宽度的2/1 )
        DUN_1_LEFT = this.realy_size.width / 2 - (PAI_WIDTH*3) + PAI_WIDTH / 2;
        //开始绘制桌面元素
        green( this );
        /****启动ws-room-realy计时器,开始监听房间信息**********************************************************************/
        this.schedule( this.draw_timer_room_realy , GAME_WS_TIME );
        /****启动ws-room-realy计时器,开始监听房间信息**********************************************************************/
        return true;
    },
    //绘制胡息
    draw_huxiANDscore:function ( log_data , is_chonghui ) {
        green_draw_huxiANDscore_replace( this , log_data , is_chonghui );
    },
    //重绘桌面底牌
    draw_dipai:function(){
        green_draw_dipai( this );
    },
    //绘制剩余底牌
    draw_dipai_number:function(){
        green_draw_dipai_number( this );
    },
    //ws-绘制新进用户头像以及名称等 - 14
    ws_user_welcome:function( DATA ){
        green_draw_userFaceTEXT( this , DATA );
    },
    //ws-开始游戏，已经获取到本方的手牌信息
    draw_play_start:function( SP , DP , LP ){
        cc.log( LP );
        for( var i = 0 ; i < LP.length ; i++ ){if( LP[i].length == 3 ){if( LP[i][0] == LP[i][1] && LP[i][1] == LP[i][2] ){JINTUO.push( LP[i][0] );}}}
        //置换key - BIG_PAI的key从1起
        for( var i = 0 ; i < LP.length ; i++ ){BIG_PAI[i+1] = LP[i];}
        BIG_DUN = BIG_PAI.length;
        //计算本次手牌共会出现几蹲（暂时不计算蹲，默认为8蹲，满足21张的空间）
        var pai_index = 1;
        for( var i = 1 ; i < BIG_PAI.length ; i++ ){
            for( var y = 0 ; y < BIG_PAI[i].length ; y++ ){
                var temp     = cc.Sprite.create( "res/pai/" + PAI_HAO[BIG_PAI[i][y]]+ ".png" );
                temp.dijidun = i;//牌的墩位
                temp.jilu_x  = temp.x;
                temp.jilu_y  = temp.y;
                temp.name    = "dd" + pai_index;
                temp.dijige  = y;//牌在该墩位处于第几个，0开始
                temp.paihao  = BIG_PAI[i][y];//牌号，也就是牌的数字
                temp.yidongzhong = false;//默认移动中状态为false
                this.PPP_pai[pai_index] = temp;//将牌信息置入到全局中
                //给每张牌绑定一个拖动事件
                var self = this;
                var temp_listen = cc.EventListener.create({
                    event:cc.EventListener.TOUCH_ONE_BY_ONE,
                    swallowTouches:true,
                    onTouchBegan: function (touch, event) {
                        var target = event.getCurrentTarget();
                        var locationInNode = target.convertToNodeSpace(touch.getLocation());
                        var s      = target.getContentSize();
                        var rect   = cc.rect(0, 0, s.width, s.height);
                        if (cc.rectContainsPoint(rect, locationInNode) && JINTUO.indexOf( target.paihao ) < 0 ) {
                            if( GAME_OVER == true ){
                                cc.log( "当前游戏已结束，不允许牌面拖动" );
                                return false;
                            }
                            target.setLocalZOrder( target.default_Zorder * 200 );
                            cc.log( "您点击了第"+ target.dijidun +"蹲的第"+ target.dijige +"个，层级编号为：" + target.default_Zorder );
                            return true;
                        }else{
                            return false;
                        }
                    },
                    onTouchMoved:function(touch,event){
                        var target     = event.getCurrentTarget();
                        var touchPoint = touch.getLocation();
                        //被允许拖动牌的范围内
                        if( touchPoint.y >= 20 && touchPoint.y <= 600 && touchPoint.x >= 60 && touchPoint.x <= 1220){
                            //是否为禁拖数字(三坎 或 斤)
                            if( JINTUO.indexOf( target.paihao ) >= 0 ){

                            }else{
                                //拖动牌
                                target.setPosition(touchPoint);
                                /****************************  当前蹲，采用对调法，  ************************/
                                /****************************  2张对调法  *************************************/
                                //一个牌被拖动以后，判断当前蹲上还有几个牌，如果只有他自己一个牌了，不做动作
                                if( DUN_PS[ target.dijidun - 1 ] == 1 ){
                                    //单排禁止做排序操作
                                    //除去有4个牌，有4个牌是不可能被拖动的，所以这里含自身顶多3张牌
                                }else if( DUN_PS[ target.dijidun - 1 ] == 2 ){
                                    cc.log("dddddddddddddddddddd2222");
                                    //如果当前顺序为第0张（从下往上0,1,2）
                                    if( target.dijige == 0 ){
                                        //如果为最底下一张。那么上面那张肯定是要掉下来；再判断是否还有第三章，有也掉下来
                                        for( var i = self.PPP_pai.length - 1; i >= 1 ; i-- ){
                                            if( self.PPP_pai[i].dijidun == target.dijidun && self.PPP_pai[i].dijige == 1 ){
                                                //第二张向下补充
                                                self.PPP_pai[i].y      = PAI_BOTOTM;
                                                target.yidongzhong     = true;
                                                //交换层级个数
                                                target.dijige          = 1;
                                                self.PPP_pai[i].dijige = 0;
                                                //交换位置
                                                var z1_y = self.PPP_pai[i].jilu_y;
                                                var z2_y = target.jilu_y;
                                                target.jilu_y = z1_y;
                                                self.PPP_pai[i].jilu_y = z2_y;
                                                //交换层级
                                                var y1 = self.PPP_pai[i].default_Zorder;
                                                var y2 = target.default_Zorder;
                                                self.PPP_pai[i].default_Zorder = y2;
                                                self.PPP_pai[i].setLocalZOrder( y2 );
                                                target.default_Zorder =  y1;
                                                target.setLocalZOrder( y1 );
                                                break;
                                            }
                                        }
                                    }
                                }else if( DUN_PS[ target.dijidun - 1 ] == 3 ){
                                    //如果当前点的是第0张（从下往上0,1,2）
                                    if( target.dijige == 0 ){
                                        cc.log(  target.dijige );
                                        //上面两张都要进行掉落,掉第二张
                                        for( var i = self.PPP_pai.length - 1; i >= 1 ; i-- ){
                                            if( self.PPP_pai[i].dijidun == target.dijidun && self.PPP_pai[i].dijige == 1 ){
                                                //第二张向下补充
                                                self.PPP_pai[i].y      = PAI_BOTOTM;
                                                target.yidongzhong     = true;
                                                //交换层级个数
                                                target.dijige          = 1;
                                                self.PPP_pai[i].dijige = 0;
                                                //交换位置
                                                var z1_y = self.PPP_pai[i].jilu_y;
                                                var z2_y = target.jilu_y;
                                                target.jilu_y = z1_y;
                                                self.PPP_pai[i].jilu_y = z2_y;
                                                //交换层级
                                                var y1 = self.PPP_pai[i].default_Zorder;
                                                var y2 = target.default_Zorder;
                                                self.PPP_pai[i].default_Zorder = y2;
                                                self.PPP_pai[i].setLocalZOrder( y2 );
                                                target.default_Zorder =  y1;
                                                target.setLocalZOrder( y1 );
                                                break;
                                            }
                                        }
                                        //上面两张都要进行掉落,掉第三张
                                        for( var i = self.PPP_pai.length - 1; i >= 1 ; i-- ){
                                            if( self.PPP_pai[i].dijidun == target.dijidun && self.PPP_pai[i].dijige == 2 ){
                                                //第二张向下补充
                                                self.PPP_pai[i].y      = PAI_BOTOTM + PAI_JIANJU;
                                                target.yidongzhong     = true;
                                                //交换层级个数
                                                target.dijige          = 2;
                                                self.PPP_pai[i].dijige = 1;
                                                //交换位置
                                                var z1_y = self.PPP_pai[i].jilu_y;
                                                var z2_y = target.jilu_y;
                                                target.jilu_y = z1_y;
                                                self.PPP_pai[i].jilu_y = z2_y;
                                                //交换层级
                                                var y1 = self.PPP_pai[i].default_Zorder;
                                                var y2 = target.default_Zorder;
                                                self.PPP_pai[i].default_Zorder = y2;
                                                self.PPP_pai[i].setLocalZOrder( y2 );
                                                target.default_Zorder =  y1;
                                                target.setLocalZOrder( y1 );
                                                break;
                                            }
                                        }
                                        //如果当前点的是第二张，则直接跟第三张进行交换操作
                                    }else if( target.dijige == 1 ){
                                        for( var i = self.PPP_pai.length - 1; i >= 1 ; i-- ) {
                                            if (self.PPP_pai[i].dijidun == target.dijidun && self.PPP_pai[i].dijige == 2) {
                                                //第二张向下补充
                                                self.PPP_pai[i].y  = PAI_BOTOTM + PAI_JIANJU;
                                                target.yidongzhong = true;
                                                //交换层级个数
                                                target.dijige = 2;
                                                self.PPP_pai[i].dijige = 1;
                                                //交换位置
                                                var z1_y      = self.PPP_pai[i].jilu_y;
                                                var z2_y      = target.jilu_y;
                                                target.jilu_y = z1_y;
                                                self.PPP_pai[i].jilu_y = z2_y;
                                                //交换层级
                                                var y1 = self.PPP_pai[i].default_Zorder;
                                                var y2 = target.default_Zorder;
                                                self.PPP_pai[i].default_Zorder = y2;
                                                self.PPP_pai[i].setLocalZOrder( y2 );
                                                target.default_Zorder =  y1;
                                                target.setLocalZOrder( y1 );
                                                break;
                                            }
                                        }
                                    }
                                }
                                /****************************  3张对调法  *************************************/
                            }
                        }
                    },
                    onTouchEnded:function(touch,event){
                        var target     = event.getCurrentTarget();
                        var touchPoint = touch.getLocation();
                        if( touchPoint.y <= LINE_CHUPAI ){
                            target.x = target.jilu_x;
                            target.y = target.jilu_y;
                            target.yidongzhong == false;
                            target.setLocalZOrder( target.default_Zorder  );
                            /* 如果牌没有打掉，且移动到了另一顿的上面的话；则需要进行吨数重改操作；一顿上面最大允许放4张牌  -- 这个只需要判断x值的位置即可 */
                            var temp_dun = self.chazhaoDun( touchPoint.x );
                            /* 判断比当前蹲小的那1蹲是否有数据，没有则需要前移 (不为0的蹲，只能大于1，大于等于2，则回至为不为0的蹲+ 1) */
                            var noEmpty  = 0;
                            for( var z = 0 ; z < DUN_PS.length ; z++ ){
                                if( DUN_PS[z] >= 1 ){
                                    noEmpty++;
                                }
                            }
                            if( temp_dun > (noEmpty + 1) ){
                                temp_dun = noEmpty + 1;
                            }
                            /* 判断 */
                            switch( temp_dun ){
                                case 1: temp_x = DUN_1_LEFT;break;
                                case 2: temp_x = DUN_1_LEFT + PAI_WIDTH;break;
                                case 3: temp_x = DUN_1_LEFT + PAI_WIDTH * 2;break;
                                case 4: temp_x = DUN_1_LEFT + PAI_WIDTH * 3;break;
                                case 5: temp_x = DUN_1_LEFT + PAI_WIDTH * 4;break;
                                case 6: temp_x = DUN_1_LEFT + PAI_WIDTH * 5;break;
                                case 7: temp_x = DUN_1_LEFT + PAI_WIDTH * 6;break;
                                case 8: temp_x = DUN_1_LEFT + PAI_WIDTH * 7;break;
                                default:
                                    cc.log('当前没有找到坐标：' + touchPoint.x + "对应的蹲，牌回到初始地点");
                                    break;
                            }
                            //判断是否拖到了坎或斤上面
                            if( DUN_NO_TUO.indexOf( temp_dun-1 ) >= 0 ){
                                cc.log('不能把牌拖到砍或者金上面去~');return false;
                            }
                            //判断是否为跟其他蹲置换
                            if( temp_dun != 0 && temp_dun != target.dijidun ){
                                //调换蹲上是否已经有4张牌了，大于等于4张，不允许被调换
                                if( DUN_PS[temp_dun-1] >= 4 ){
                                    cc.log( "该吨位已经存在4张牌，不能在继续添加了" );return true;
                                }
                                //尝试使用层级重设 . 查找新蹲上已经有几个了；那就分别设置为4，3，2 ；最新的那个设置为最小的1就可以了。
                                for( var i = 1 ; i <= DUN_PS[temp_dun-1] ; i++ ){
                                    for( var y = self.PPP_pai.length - 1; y >= 1 ; y-- ){
                                        if( self.PPP_pai[y].dijidun == temp_dun && self.PPP_pai[y].dijige == (i-1) && (i-1) == 0 ){
                                            self.PPP_pai[y].default_Zorder = 4;
                                            self.PPP_pai[y].setLocalZOrder( 4 );
                                        }
                                        if( self.PPP_pai[y].dijidun == temp_dun && self.PPP_pai[y].dijige == (i-1) && (i-1) == 1 ){
                                            self.PPP_pai[y].default_Zorder = 3;
                                            self.PPP_pai[y].setLocalZOrder( 3 );
                                        }
                                        if( self.PPP_pai[y].dijidun == temp_dun && self.PPP_pai[y].dijige == (i-1) && (i-1) == 2 ){
                                            self.PPP_pai[y].default_Zorder = 2;
                                            self.PPP_pai[y].setLocalZOrder( 2 );
                                        }
                                    }
                                }
                                //修改当前牌的层级索引信息
                                target.default_Zorder = 1;
                                target.setLocalZOrder( 1 );
                                //修改吨数上的牌数修改
                                DUN_PS[ (target.dijidun-1) ]--;
                                DUN_PS[ (temp_dun-1) ]++;
                                //修改被拖动牌位置信息
                                target.y = PAI_BOTOTM + ( (DUN_PS[temp_dun-1] - 1) * PAI_JIANJU );
                                target.x = temp_x;
                                target.jilu_y  = target.y;
                                target.jilu_x  = target.x;
                                //临时记录被移动之前该牌所在的牌位
                                var temp_dunnumber    = target.dijidun;
                                var temp_dijigenumber = target.dijige;
                                //修改当前被拖动牌的个数索引以及蹲号
                                target.dijige  = (DUN_PS[temp_dun-1]) - 1;
                                target.dijidun = temp_dun;
                                //拖放完成后，需要检查是否该蹲上面还有牌，没有牌，需要进行左右侧移动操作
                                if( DUN_PS[(temp_dunnumber-1)] <= 0 ) {
                                    self.yidong_pai( temp_dunnumber );
                                }else{
                                    //如果还有3张牌，则进行第几个的索引重排操作
                                    if( DUN_PS[(temp_dunnumber-1)] == 3 && temp_dijigenumber != 3 ){
                                        cc.log( temp_dijigenumber + "==" + DUN_PS[(temp_dunnumber-1)] );
                                        if( temp_dijigenumber == 2 ){
                                            for( var y = self.PPP_pai.length - 1; y >= 1 ; y-- ){
                                                if( self.PPP_pai[y].dijidun == temp_dunnumber && self.PPP_pai[y].dijige == 3 ){
                                                    self.PPP_pai[y].dijige = 2;
                                                }
                                            }
                                        }else{
                                            for( var y = self.PPP_pai.length - 1; y >= 1 ; y-- ){
                                                if( self.PPP_pai[y].dijidun == temp_dunnumber && self.PPP_pai[y].dijige == 1 ){
                                                    self.PPP_pai[y].dijige = 0;
                                                }
                                                if( self.PPP_pai[y].dijidun == temp_dunnumber && self.PPP_pai[y].dijige == 2 ){
                                                    self.PPP_pai[y].dijige = 1;
                                                }
                                                if( self.PPP_pai[y].dijidun == temp_dunnumber && self.PPP_pai[y].dijige == 3 ){
                                                    self.PPP_pai[y].dijige = 2;
                                                }
                                            }
                                        }
                                    }
                                }
                                //进行一次所有牌的重新排列操作
                                self.allPaiSort();
                            }
                        }else{
                            //判断是否有权限打牌
                            if( PB_USER_UID != GAME_PUT_UID ) {
                                target.x = target.jilu_x;
                                target.y = target.jilu_y;
                                target.yidongzhong == false;
                                target.setLocalZOrder(target.default_Zorder);
                                cc.log("当前非你打牌，你暂时没有权限把打出去==============================================");
                                return false;
                            }
                            //将牌由原点升到中心点
                            var mx = size.width / 2;
                            var my = size.height / 2 + 75;
                            var moveTo = cc.MoveTo.create( 0.1 , cc.p( mx , my ) );
                            target.setScale( 1.15 );
                            target.runAction( moveTo );
                            //出子后理牌
                            draw_play_paireset();
                            //发送出牌消息给服务端,本次出牌工作完成,等待服务端生成新的消息
                            PB_WS.send("c:" + PB_USER_UID + ":" + ROOM_ID  + ":" + target.paihao);
                            return false;
                        }
                    }
                });
                cc.eventManager.addListener( temp_listen ,  this.PPP_pai[pai_index] );
                this.addChild( this.PPP_pai[pai_index] );pai_index++;
            }
        }
        //显示所有牌,也就是处理发牌的效果(先把所有牌都集合到中心点,再依次发出来) - 最大5蹲，最小也会有4蹲
        var defaulZo     = 1;
        var size         = cc.director.getWinSize();
        //发牌效果
        for( var i = this.PPP_pai.length - 1; i >= 1 ; i-- ){
            //设置默认为中心发牌点
            this.PPP_pai[i].x = size.width / 2 - 10;
            this.PPP_pai[i].y = 500;
            var m_x = 0;
            var m_y = 0;
            //将不能拖动的牌面变灰&并设置不能拖放牌的顿号
            if( JINTUO.indexOf( this.PPP_pai[i].paihao ) >= 0 ){
                this.PPP_pai[i].setColor(cc.color(150,150,150))
                this.PPP_pai[i].setOpacity(250);
                if( DUN_NO_TUO.indexOf( (this.PPP_pai[i].dijidun - 1) ) < 0 ) {
                    DUN_NO_TUO.push(this.PPP_pai[i].dijidun - 1);
                }
            }
            //处理第1蹲牌
            if( this.PPP_pai[i].dijidun == 1 ){
                m_x = DUN_1_LEFT;
                m_y = PAI_BOTOTM + ( this.PPP_pai[i].dijige * PAI_JIANJU );
                DUN_PS[0]++;
            }
            //处理第2蹲牌
            if( this.PPP_pai[i].dijidun == 2 ){
                m_x = DUN_1_LEFT + PAI_WIDTH;
                m_y = PAI_BOTOTM + ( this.PPP_pai[i].dijige * PAI_JIANJU );
                DUN_PS[1]++;
            }
            //处理第3蹲牌
            if( this.PPP_pai[i].dijidun == 3 ){
                m_x = DUN_1_LEFT + PAI_WIDTH * 2;
                m_y = PAI_BOTOTM + ( this.PPP_pai[i].dijige * PAI_JIANJU );
                DUN_PS[2]++;
            }
            //处理第4蹲牌
            if( this.PPP_pai[i].dijidun == 4 ){
                m_x = DUN_1_LEFT + PAI_WIDTH * 3;
                m_y = PAI_BOTOTM + ( this.PPP_pai[i].dijige * PAI_JIANJU );
                DUN_PS[3]++;
            }
            //处理第5蹲牌
            if( this.PPP_pai[i].dijidun == 5 ){
                m_x = DUN_1_LEFT + PAI_WIDTH * 4;
                m_y = PAI_BOTOTM + ( this.PPP_pai[i].dijige * PAI_JIANJU );
                DUN_PS[4]++;
            }
            //处理第6蹲牌
            if( this.PPP_pai[i].dijidun == 6 ){
                m_x = DUN_1_LEFT + PAI_WIDTH * 5;
                m_y = PAI_BOTOTM + ( this.PPP_pai[i].dijige * PAI_JIANJU );
                DUN_PS[5]++;
            }
            //处理第7蹲牌
            if( this.PPP_pai[i].dijidun == 7 ){
                m_x = DUN_1_LEFT + PAI_WIDTH * 6;
                m_y = PAI_BOTOTM + ( this.PPP_pai[i].dijige * PAI_JIANJU );
                DUN_PS[6]++;
            }
            //处理第8蹲牌
            if( this.PPP_pai[i].dijidun == 8 ){
                m_x = DUN_1_LEFT + PAI_WIDTH * 7;
                m_y = PAI_BOTOTM + ( this.PPP_pai[i].dijige * PAI_JIANJU );
                DUN_PS[7]++;
            }
            //重新改变记录牌的位置
            this.PPP_pai[i].jilu_x = m_x;
            this.PPP_pai[i].jilu_y = m_y;
            //改变牌的大小
            self.PPP_pai[i].setScale(1.25);
            //动作移牌
            var moveTo = cc.MoveTo.create( 0.2 , cc.p( m_x , m_y ) );
            self.PPP_pai[i].runAction( moveTo );
            //改变牌的层级展示顺序&并记录
            this.PPP_pai[i].default_Zorder = defaulZo;
            this.PPP_pai[i].setLocalZOrder( defaulZo );
            defaulZo++;
        }
        this.chuchihusDUn();
    },
    //对所有牌进行一次排序跟层级重设操作
    allPaiSort:function(){
        //遍历所有蹲
        var self = this;
        for( var i  = 0 ; i < DUN_PS.length ; i++ ){
            //设置临时顿号
            var temp_dunnumber = i + 1;
            //如果该蹲上存在牌的情况下；则进行0.1.2.3的牌的排序操作
            if( DUN_PS[i] >= 1 ){
                for( var y = self.PPP_pai.length - 1; y >= 1 ; y-- ) {
                    if (self.PPP_pai[y].dijidun == temp_dunnumber && self.PPP_pai[y].dijige == 0) {
                        self.PPP_pai[y].default_Zorder = 4;
                        self.PPP_pai[y].setLocalZOrder(4);
                        self.PPP_pai[y].y      = PAI_BOTOTM;
                        self.PPP_pai[y].jilu_y = PAI_BOTOTM;
                    }
                    if (self.PPP_pai[y].dijidun == temp_dunnumber && self.PPP_pai[y].dijige == 1) {
                        self.PPP_pai[y].default_Zorder = 3;
                        self.PPP_pai[y].setLocalZOrder(3);
                        self.PPP_pai[y].y      = PAI_BOTOTM + PAI_JIANJU;
                        self.PPP_pai[y].jilu_y = PAI_BOTOTM + PAI_JIANJU;
                    }
                    if (self.PPP_pai[y].dijidun == temp_dunnumber && self.PPP_pai[y].dijige == 2) {
                        self.PPP_pai[y].default_Zorder = 2;
                        self.PPP_pai[y].setLocalZOrder(2);
                        self.PPP_pai[y].y      = PAI_BOTOTM + PAI_JIANJU * 2;
                        self.PPP_pai[y].jilu_y = PAI_BOTOTM + PAI_JIANJU * 2;
                    }
                    if (self.PPP_pai[y].dijidun == temp_dunnumber && self.PPP_pai[y].dijige == 3) {
                        self.PPP_pai[y].default_Zorder = 1;
                        self.PPP_pai[y].setLocalZOrder(1);
                        self.PPP_pai[y].y      = PAI_BOTOTM + PAI_JIANJU * 3;
                        self.PPP_pai[y].jilu_y = PAI_BOTOTM + PAI_JIANJU * 3;
                    }
                }
            }
        }
    },
    chuchihusDUn:function(){
        for( var i = 1 ; i <= DUNSHU ; i++ ){
            JILU_TC[i] = new Array();
            JILU_TC[i].min = DUN_1_LEFT + ( (i-1) * PAI_WIDTH) - LR_CHAZHI;
            JILU_TC[i].max = DUN_1_LEFT + ( (i-1) * PAI_WIDTH) + LR_CHAZHI;
        }
    },
    chazhaoDun:function( t_x ){
        for( var i = 1 ; i < JILU_TC.length ; i++ ){
            if( t_x >= JILU_TC[i].min && t_x <= JILU_TC[i].max ){
                return i;
            }
        }
        return 0;
    },
    //牌空后的移动操作
    yidong_pai:function( dijidun ){
        var self = this;
        var m_x  = 0;
        var m_y  = 0;
        //如果当前蹲被打完或者移动完；所有牌都往左侧移动76个位置，并且蹲好-1
        for( var i = self.PPP_pai.length - 1; i >= 1 ; i-- ){
            if( self.PPP_pai[i].dijidun > dijidun ) {
                m_x = self.PPP_pai[i].x - PAI_WIDTH;
                m_y = self.PPP_pai[i].y;
                var moveTo = cc.MoveTo.create(0.1, cc.p(self.PPP_pai[i].x - PAI_WIDTH , self.PPP_pai[i].y));
                self.PPP_pai[i].runAction(moveTo);
                //同时需要改变当前的x跟y值
                self.PPP_pai[i].jilu_x  = m_x;
                self.PPP_pai[i].jilu_y  = m_y;
                //将被移动过来的蹲号全部改为新顿号
                self.PPP_pai[i].dijidun = self.PPP_pai[i].dijidun - 1;
            }
        }
        //DUN_PS 数组重排（也就是改变值的偏移量） - 其实就是往前推
        DUN_PS.push(0);
        DUN_PS.splice( (dijidun-1) , 1);
    },
    /*************************  出牌相关  *******************************************************************************/
    draw_play_put:function( userid , cardid ){
        var size = cc.director.getWinSize();
        //如果是自己出牌(这里可能分为重连或者正常情况::总之我们把牌显示在中间就行了，都不影响)
        //但本家需要从自己的手牌中移除这张牌
        if( userid == PB_USER_UID ){
            this.MP_P     = cc.Sprite.create( "res/pai/" + PAI_HAO[cardid]+ ".png" );
            this.MP_P.setScale( 1.15 );
            this.MP_P.x   = size.width / 2;
            this.MP_P.y   = size.height / 2 + 75;
            this.addChild( this.MP_P );
            GAME_OUT_PAI.push( this.MP_P );
            for( var i = this.PPP_pai.length - 1; i >= 1 ; i-- ){
                if( this.PPP_pai[i].paihao == cardid ){
                    //如果本墩的墩上面有大于等于2张，则要进行是否掉落判断
                    if( DUN_PS[ this.PPP_pai[i].dijidun - 1 ] >= 2 ){
                        if( this.PPP_pai[i].dijige == 2 ){//如果是第三个被移除了，不管他
                        }
                        if( this.PPP_pai[i].dijige == 1 && DUN_PS[ this.PPP_pai[i].dijidun - 1 ] == 2 ){//如果是第二张被移除掉了，但刚好只有2个的情况下；也不需要做操作

                        }
                        if( this.PPP_pai[i].dijige == 1 && DUN_PS[ this.PPP_pai[i].dijidun - 1 ] == 3 ){//如果有三张的情况下，第二个被移除了，则第三个需要进行掉落
                            for( var x = this.PPP_pai.length - 1; x >= 1 ; x-- ){
                                if( this.PPP_pai[i].dijidun == this.PPP_pai[x].dijidun && this.PPP_pai[x].dijige == 2 ){
                                    if( this.PPP_pai[x].dijige == 2 ){
                                        this.PPP_pai[x].y      = this.PPP_pai[i].y - 40;
                                        this.PPP_pai[x].jilu_y = this.PPP_pai[i].jilu_y;
                                        this.PPP_pai[x].dijige = this.PPP_pai[i].dijige;
                                        this.PPP_pai[x].dijidun= this.PPP_pai[i].dijidun;
                                        this.PPP_pai[x].default_Zorder = this.PPP_pai[i].default_Zorder;
                                        this.PPP_pai[x].setLocalZOrder( this.PPP_pai[i].default_Zorder );
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    //本蹲的墩数-1
                    DUN_PS[ this.PPP_pai[i].dijidun - 1 ] --;
                    //移除本张牌
                    this.removeChild( this.PPP_pai[i] );
                    this.PPP_pai.splice( i , 1 );
                    break;
                }
            }
        }
        //如果是上家牌
        if( userid == ROOM_WS_SJ ){
            cc.log("上架出牌了");
            this.MP_P     = cc.Sprite.create( "res/pai/" + PAI_HAO[cardid]+ ".png" );
            this.MP_P.setScale(0.6);
            this.MP_P.x   = 75;
            this.MP_P.y   = size.height - 150;
            this.MP_K     = cc.Sprite.create( "res/mopai_bj.png" );
            this.MP_K.setScale(0.6);
            this.MP_K.x   = 75;
            this.MP_K.y   = size.height - 150;
            this.addChild( this.MP_K );
            this.addChild( this.MP_P );
            this.MP_P.runAction( cc.MoveTo.create( 0.2 , cc.p(  220, size.height - 180 ) ) );
            this.MP_K.runAction( cc.MoveTo.create( 0.2 , cc.p(  220 , size.height - 180 ) ) );
            GAME_OUT_PAI.push( this.MP_P );GAME_OUT_PAI.push( this.MP_K );
        }
        //如果是下家出牌
        if( userid == ROOM_WS_XJ ){
            cc.log("222上架出牌了");
            this.MP_P     = cc.Sprite.create( "res/pai/" + PAI_HAO[cardid]+ ".png" );
            this.MP_P.setScale(0.6);
            this.MP_P.x   = size.width - 75;
            this.MP_P.y   = 150;
            this.MP_K     = cc.Sprite.create( "res/mopai_bj.png" );
            this.MP_K.setScale(0.6);
            this.MP_K.x   = size.width - 75;
            this.MP_K.y   = 150;
            this.addChild( this.MP_K );
            this.addChild( this.MP_P );
            this.MP_P.runAction( cc.MoveTo.create( 0.2 , cc.p( size.width - 220 , 230 ) ) );
            this.MP_K.runAction( cc.MoveTo.create( 0.2 , cc.p( size.width - 220 , 230 ) ) );
            GAME_OUT_PAI.push( this.MP_P );GAME_OUT_PAI.push( this.MP_K );
        }
        //如果是对家出牌
        if( userid == ROOM_WS_DM ){
            cc.log("33333上架出牌了");
            this.MP_P     = cc.Sprite.create( "res/pai/" + PAI_HAO[cardid]+ ".png" );
            this.MP_P.setScale(0.6);
            this.MP_P.x   = size.width - 75;
            this.MP_P.y   = size.height - 150;
            this.MP_K     = cc.Sprite.create( "res/mopai_bj.png" );
            this.MP_K.setScale(0.6);
            this.MP_K.x   = size.width - 75;
            this.MP_K.y   = size.height - 150;
            this.addChild( this.MP_K );
            this.addChild( this.MP_P );
            this.MP_P.runAction( cc.MoveTo.create( 0.2 , cc.p( size.width - 220 , size.height - 180 ) ) );
            this.MP_K.runAction( cc.MoveTo.create( 0.2 , cc.p( size.width - 220 , size.height - 180 ) ) );
            GAME_OUT_PAI.push( this.MP_P );GAME_OUT_PAI.push( this.MP_K );
        }
        //进行牌的复位操作
        draw_play_paireset();
    },
    /**
     *
     * @param card_number   碰的牌的号码
     * @param card_input
     */
    draw_play_peng_firm:function( card_number , card_input ){
	    delete_all_btn();delete_all_bg();
        for( var i = 0 ; i < GAME_OUT_PAI.length ; i++ ){this.removeChild( GAME_OUT_PAI[i] );}
        var xiaopai_temp = new Array();
        var xiaopai_name = card_number <= 10 ? 'ss' + card_number : 's' + (card_number-10);
        if( card_input == PB_USER_UID ){//如果是自己的碰
            for( var i = this.PPP_pai.length - 1; i >= 1 ; i-- ) {//清楚手牌中的2个牌
                if( this.PPP_pai[i].paihao == card_number ){
                    this.removeChild( this.PPP_pai[i] );
                    DUN_PS[this.PPP_pai[i].dijidun - 1]--;//牌墩上的牌数-1
                    this.PPP_pai.splice(i, 1);
                }
            }
            for( var i =0; i <= 2 ; i++ ){//添加3张小牌
                xiaopai_temp[i] = cc.Sprite.create( "res/pai/xp/" + xiaopai_name + ".png" );
                xiaopai_temp[i].setScale( 1.5 );
                xiaopai_temp[i].x = this.realy_size.width / 2;
                xiaopai_temp[i].y = this.realy_size.height / 2 +  SMALL_PAI_H*1.5*(i);
                var action1 = cc.MoveTo.create( 0.3 , cc.p( 40 * ZJ_XP_COUNT , SMALL_PAI_H*(i) - 500 + LB_XP_HEIGHT ) )
                var action2 = cc.scaleTo(0.3 , 1 );
                xiaopai_temp[i].runAction( cc.spawn(action1, action2 ) );
                this.addChild( xiaopai_temp[i] , 1 );
                //添加碰牌记录
                xiaopai_temp[i].card_number = card_number;PENG_PAI_ZJ.push( xiaopai_temp[i] );
                MISCE.push( xiaopai_temp[i] );//添加杂项
            }
            ZJ_XP_COUNT++;
        }
        if( card_input == ROOM_WS_XJ ){
            for( var i =0; i <= 2 ; i++ ){//添加3张小牌(下家区域)
                xiaopai_temp[i] = cc.Sprite.create( "res/pai/xp/" + xiaopai_name + ".png" );
                xiaopai_temp[i].x = this.realy_size.width * 0.7;
                xiaopai_temp[i].y = SMALL_PAI_H * i + this.realy_size.height / 2;
                var action1 = cc.MoveTo.create( 0.2 , cc.p( this.realy_size.width -  40 * XJ_XP_COUNT , SMALL_PAI_H * i + RB_XP_HEIGHT ) )
                var action2 = cc.scaleTo(0.2 , 1 );
                xiaopai_temp[i].runAction( cc.spawn(action1, action2 ) );
                this.addChild( xiaopai_temp[i] , 1 );
                //添加碰牌记录
                xiaopai_temp[i].card_number = card_number;PENG_PAI_XJ.push( xiaopai_temp[i] );
                MISCE.push( xiaopai_temp[i] );//添加杂项
            }
            XJ_XP_COUNT++;
        }
        if( card_input == ROOM_WS_SJ ){
            for( var i =0; i <= 2 ; i++ ){//添加3张小牌(上家区域)
                xiaopai_temp[i] = cc.Sprite.create( "res/pai/xp/" + xiaopai_name + ".png" );
                xiaopai_temp[i].x = this.realy_size.width  * 0.3;
                xiaopai_temp[i].y = this.realy_size.height * 0.7 -  SMALL_PAI_H * i;
                var action1 = cc.MoveTo.create( 0.2 , cc.p( 40 * SJ_XP_COUNT , this.realy_size.height - TOP_LEFT_XP_HEIGHT -  SMALL_PAI_H*i ) )
                var action2 = cc.scaleTo(0.2 , 1 );
                xiaopai_temp[i].runAction( cc.spawn(action1, action2 ) );
                this.addChild( xiaopai_temp[i] , 1 );
                //添加碰牌记录
                xiaopai_temp[i].card_number = card_number;PENG_PAI_SJ.push( xiaopai_temp[i] );
                MISCE.push( xiaopai_temp[i] );//添加杂项
            }
            SJ_XP_COUNT++;
        }
        if( card_input == ROOM_WS_DM ){
            for( var i =0; i <= 2 ; i++ ){//添加3张小牌(对门区域)
                xiaopai_temp[i] = cc.Sprite.create( "res/pai/xp/" + xiaopai_name + ".png" );
                xiaopai_temp[i].x = this.realy_size.width * 0.7;
                xiaopai_temp[i].y = this.realy_size.height * 0.7 -  SMALL_PAI_H * i;
                var action1 = cc.MoveTo.create( 0.2 , cc.p( this.realy_size.width -  40 * DM_XP_COUNT , this.realy_size.height - TOP_LEFT_XP_HEIGHT -  SMALL_PAI_H*i ) )
                var action2 = cc.scaleTo(0.2 , 1 );
                xiaopai_temp[i].runAction( cc.spawn(action1, action2 ) );
                this.addChild( xiaopai_temp[i] , 1 );
                //添加碰牌记录
                xiaopai_temp[i].card_number = card_number;PENG_PAI_DM.push( xiaopai_temp[i] );
                MISCE.push( xiaopai_temp[i] );//添加杂项
            }
            DM_XP_COUNT++;
        }
        GAME_NOW_BTN_C = 0;
        this.BTN_CLOSE = null;
        //进行trip操作
        show_tripAND_sound( 'peng' , card_input );
        draw_play_paireset();
    },
    //ws-碰牌取消
    //参数：这个牌是谁打出来的
    draw_play_peng_fail:function( input_uid ,  card_number , fail_uid  ){
        if( fail_uid == PB_USER_UID ){//如果是本人取消的，则要把按钮全部销毁
            this.removeChild( this.BTN_CHI );
            this.removeChild( this.BTN_PENG );
            this.removeChild( this.BTN_CLOSE );
            GAME_NOW_BTN_C = 0;
        }
        if( GAME_IS_CHI == 1 ){//如果还有人要吃这个牌的情况下，等待吃牌人做操作
            return true;
        }else{//碰牌取消后，如果没人再可以吃这个牌了；则进行牌位移动操作
            add_bp( card_number , this.MP_P.x , this.MP_P.y , input_uid , true );
        }
    },
    //ws-取消吃牌事件；取消吃牌也要判断当前是否还有人能碰，要是有人能碰的情况下；还是不能做界面操作
    //但是如果是自己取消的话；至少是要先把按钮给清除掉
    //参数，出牌人，出牌号，取消人
    draw_play_chi_fail:function( input_uid , card_number , fail_uid ){
        if( fail_uid == PB_USER_UID ){
            this.removeChild( this.BTN_CHI );
            this.removeChild( this.BTN_PENG );
            this.removeChild( this.BTN_CLOSE );
            GAME_NOW_BTN_C = 0;
        }
        if( GAME_IS_PENG == 1 ){
            return true;//当前还有人处于碰未取消的状态下；那么界面就不做操作
        }else{
            add_bp( card_number , this.MP_P.x , this.MP_P.y , input_uid , true );
        }
    },
    /*************************  ws监听计时器,用于不间断询问房间最新信息，若得到回复则进行画面操作 *****************************/
    draw_timer_room_realy:function(){
        cc.log("正在询问是否牌局"+ ROOM_ID +"最新游戏进展...." + PB_USER_UID);
        PB_WS.send("p:" + PB_USER_UID + ":" + ROOM_ID + ":" + PB_MD_NUM);
    },
    /*************************  ws监听计时器,用于不间断询问房间最新信息，若得到回复则进行画面操作 *****************************/
});
var RoomPlayScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var play      = new RoomPlayLayer();
        PL_Room_Play  = play;
        cc.log( PL_Room_Play );
        this.addChild(play);
    }
});